/* JS per l'header bar */



/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE RIEMPIMENTO CONTENUTI TAB "ATTIVI" / "COMPLETATI" / "PUBBLICATI"

/***************************************************************************************************************************/
/***************************************************************************************************************************/

/* Variabili Json */
var pathRiempimentoAttivi       =   "json/dropDown_userAttivi.json";
var pathRiempimentoCompletati   =   "json/dropDown_userCompletati.json";
var pathRiempimentoPubblicati   =   "json/dropDown_userPubblicati.json";


function fmy_riempiSubMenuBox( p_jsonPath, p_menuName ){
    $.ajax({
        url: p_jsonPath,
        dataType: "json",
        success: function( dataJson ) {
            //console.log( dataJson );
            
            sizeDataJson = ( dataJson.length );
            //  console.log(sizeDataJson);
            
            if (sizeDataJson > 0 ) 
                $( "#myMenuDropdown ul.submenu-box" ).css( "display", "block" );
            
            switch ( p_menuName ){     
                case "attivi":
                    $( "#myMenuDropdown ul.submenu-box li.item" ).addClass( p_menuName );
                break;
                case "completati":
                    $( "#myMenuDropdown ul.submenu-box li.item" ).addClass( p_menuName );
                break;
                case "pubblicati":
                    $( "#myMenuDropdown ul.submenu-box li.item" ).addClass( p_menuName );
                break;
            }
            
            
            var lastAttivo = $( "#myMenuDropdown ul.submenu-box li.item:last-child" );
            
            for( i = 0 ; i < sizeDataJson ; i++ ){
            
                //creazione variabili
                var corsoUrl         =   dataJson[i].indirizzo_url;
                var copertinaUrl     =   dataJson[i].copertina;
                var titolo           =   dataJson[i].titolo;
                var livello          =   dataJson[i].livello;
                var prezzo           =   dataJson[i].prezzo;
                var voto             =   dataJson[i].voto;
                
                
                /*inserimento dei dati negli li di lista_domande */
                lastAttivo.find( "a" ).attr( "src", corsoUrl );
                lastAttivo.find( "img" ).attr( "src", copertinaUrl );
                lastAttivo.find( ".title" ).html( titolo );
                lastAttivo.find( ".level" ).html( livello );
                lastAttivo.find( ".price" ).html( prezzo );
                
                lastAttivo.find( ".lampadine_header_xs" ).html( voto );
                new_voto = voto*2;
				new_voto = parseInt( new_voto );
				switch( new_voto ){
					case 0:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -170px");
					break;
					case 1:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -152px");
					break;
					case 2:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -136px");
					break;
					case 3:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -117px");
					break;			  
					case 4:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -100px");
					break;
					case 5:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -84px");
					break;		
                    case 6:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -66px");
					break;
                    case 7:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -49px");
					break;
                    case 8:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -32px");
					break;
                    case 9:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px -15px");
					break;
                    case 10:
						lastAttivo.find(".lampadine_header_xs").css("background-position","0px 0px");
					break;
				}
                if( i !== ( sizeDataJson - 1 ) ) {
                    $( "ul.submenu-box li.item:first-child" ).clone().appendTo( "ul.submenu-box" );
				}	
            } //end for
            $( "ul.submenu-box li.item:first-child" ).clone().appendTo( "ul.submenu-box" );
            $( "ul.submenu-box li.item:first-child" ).remove();
            
        },//end success
        
        error: function() {
         console.log( "non ho caricato il file json di uno dei tab attivi/completati/pubblicati" );
        } //end error
        
    });
} /* end riempimento tab*/




/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE RIEMPIMENTO CONTENUTI SEARCH

/***************************************************************************************************************************/
/***************************************************************************************************************************/

/* Variabili Json */
var pathSearch    =   "json/ricerca.json";


function fmy_riempiSearchBox(){
    $.ajax({
        url: pathSearch,
        dataType: "json",
        success: function( dataJson ) {
            //console.log( dataJson );
            
            sizeDataJson = ( dataJson.length );
            //  console.log(sizeDataJson);
            
            if (sizeDataJson > 0 ) 
                $( "#search_content ul.search-box" ).css( "display", "block" );
            
            
            var lastSearch= $( "#search_content ul.search-box li.item:last-child" );
            
            for( i = 0 ; i < sizeDataJson ; i++ ){
            
                //creazione variabili
                var corsoUrl         =   dataJson[i].indirizzo_url;
                var copertinaUrl     =   dataJson[i].copertina;
                var titolo           =   dataJson[i].titolo;
                var livello          =   dataJson[i].livello;
                var prezzo           =   dataJson[i].prezzo;
                var voto             =   dataJson[i].voto;
                
                
                /*inserimento dei dati negli li di lista_domande */
                lastSearch.find( "a" ).attr( "src", corsoUrl );
                lastSearch.find( "img" ).attr( "src", copertinaUrl );
                lastSearch.find( ".title" ).html( titolo );
                lastSearch.find( ".level" ).html( livello );
                lastSearch.find( ".price" ).html( prezzo );
                
                lastSearch.find( ".lampadine_header_xs" ).html( voto );
                new_voto = voto*2;
				new_voto = parseInt( new_voto );
				switch( new_voto ){
					case 0:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -170px");
					break;
					case 1:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -152px");
					break;
					case 2:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -136px");
					break;
					case 3:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -117px");
					break;			  
					case 4:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -100px");
					break;
					case 5:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -84px");
					break;		
                    case 6:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -66px");
					break;
                    case 7:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -49px");
					break;
                    case 8:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -32px");
					break;
                    case 9:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px -15px");
					break;
                    case 10:
						lastSearch.find(".lampadine_header_xs").css("background-position","0px 0px");
					break;
				}
                if( i !== ( sizeDataJson - 1 ) ) {
                    $( "ul.search-box li.item:first-child" ).clone().appendTo( "ul.search-box" );
				}	
            } //end for
            $( "ul.search-box li.item:first-child" ).clone().appendTo( "ul.search-box" );
            $( "ul.search-box li.item:first-child" ).remove();
            
        },//end success
        
        error: function() {
         console.log( "non ho caricato il file json della ricerca" );
        } //end error
        
    });
} /* end riempimento search*/






/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE DROPDOWN SEARCH

/***************************************************************************************************************************/
/***************************************************************************************************************************/

/*---- BEGIN ANIMAZIONE SLIDING CREA CORSO ----*/
function fmy_dropDownSearch () {
    $( ".search_something #search_lens" ).live("click", function () {
        $( this ).next().find( ".icon_arrow_up" ).css( "display", "block" );
		$( this ).next().slideDown( "500" );
    });    
   
    $( ".search_something .icon_arrow_up" ).live( "click" , function () {
        var rootDiv = $( this ).parent();
        $( this ).css( "display", "none" );
        rootDiv.slideUp( "500" );
    });  
}
/*---- END ANIMAZIONE SLIDING CREA CORSO ----*/





/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE DROPDOWN MENU USER

/***************************************************************************************************************************/
/***************************************************************************************************************************/

/*---- BEGIN ANIMAZIONE SLIDING CREA CORSO ----*/
function fmy_dropDownMenu () {
    $( ".dropDownMenuClose" ).live("click", function () {
        $( this ).find(".arrow").removeClass( "icon_arrow_down" ).addClass( "icon_arrow_up" );
		$( this ).next().slideDown( "500" );
        $( this ).removeClass( "dropDownMenuClose" ).addClass( "dropDownMenuOpen" );
    });    
   
    $( ".dropDownMenuOpen" ).live( "click" , function () {
        $( this ).find(".arrow").removeClass( "icon_arrow_up" ).addClass( "icon_arrow_down" );
        $( this ).next().slideUp( "500" );
        $( this ).removeClass( "dropDownMenuOpen" ).addClass( "dropDownMenuClose" );
    });  
}
/*---- END ANIMAZIONE SLIDING CREA CORSO ----*/



/*---- BEGIN APERTURA PRIMO SOTTOMENU ----*/
function fmy_displayFirstSubmenu() {
    var current = $( '#myMenuDropdown #submenu-attivi' );
    if(current.length > 0){
        current.find( 'a' ).addClass( 'submenu_title_link' );
        fmy_riempiSubMenuBox( pathRiempimentoAttivi, 'attivi' );
    }
}
/*---- END APERTURA PRIMO SOTTOMENU  ----*/


/*
function fmy_resetSubMenu() {
    $( ".submenu-box li.item div" ).empty();
}


/*---- BEGIN ANIMAZIONE TABS USER HEADER ----*/
function fmy_clickSubMenu ( p_menuName ) {
  
    //cancello gli elementi della lista tranne il primo che viene usato come template (necessario per pulire lo switch fra i submenu)
    $( 'ul.submenu-box' ).children().slice(1).detach();
    
    switch ( p_menuName ){     
        case "attivi":
            $( '.submenu-title a' ).removeClass( 'submenu_title_link' );
            $( ' #submenu-attivi a' ).addClass( 'submenu_title_link' );
            //riempie la tab Attivi
            fmy_riempiSubMenuBox( pathRiempimentoAttivi, 'attivi' );
        break;
        case "completati":
            $( '.submenu-title a' ).removeClass( 'submenu_title_link' );
            $( '#submenu-completati a').addClass( 'submenu_title_link' );
            fmy_riempiSubMenuBox( pathRiempimentoCompletati, 'completati' );  
        break;
        case "wishlist":
            $( '.submenu-title a' ).removeClass( 'submenu_title_link' );
            $( '#submenu-wishlist a' ).addClass( 'submenu_title_link' );
         // todo - community elements
        break;
        case "pubblicati":            
            $( '.submenu-title a' ).removeClass( 'submenu_title_link' );
            $( '#submenu-pubblicati a' ).addClass( 'submenu_title_link' );
            fmy_riempiSubMenuBox( pathRiempimentoPubblicati, 'pubblicati' );
        break;
    }
 }


function fmy_clickSubmenuLinks(){
    $( '#submenu-attivi' ).bind('click', function(){
        fmy_clickSubMenu( 'attivi' );
    });
    $('#submenu-completati').bind('click', function(){
        fmy_clickSubMenu( 'completati' );
    });
    $( '#submenu-wishlist' ).bind('click', function(){
        fmy_clickSubMenu( 'wishlist' );
    });
    $( '#submenu-pubblicati' ).bind('click', function(){
        fmy_clickSubMenu( 'pubblicati' );
    });  
}
/*---- END ANIMAZIONE TABS USER HEADER ----*/





/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE SIDEBAR PER IL BOX RICERCA DROPDOWN

/***************************************************************************************************************************/
/***************************************************************************************************************************/


function fmy_scroll_ListaRisultatiSearch() {
    scroll_ListaRisultati = false;
    scroll_ListaRisultati = $("#search_content ul.search-box").niceScroll({cursorcolor:"#52B5BB", cursorfixedheight: "50"});
}








/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    DOCUMENT READY

/***************************************************************************************************************************/
/***************************************************************************************************************************/

$(document).ready(function(){

    //animazione dropdown menu nell'header bar
    fmy_dropDownMenu();
    
    //visualizza il primo submenu all'apertura del dropdownmenu
    fmy_displayFirstSubmenu();
    
    //aggiornamento sezione "attivi-completati-wishlist-pubblicati" nel dorpdown menu dell'header bar
    fmy_clickSubmenuLinks();
  
    //animazione dropdown ricerca
    fmy_dropDownSearch();
    //riempimento risultati di ricerca
    fmy_riempiSearchBox();
    
    fmy_scroll_ListaRisultatiSearch();
            
}); /*---- END document ready ----*/


